/**
 * 自定义窗口移动
 */

import { screen } from 'electron';

export class AzCustomWindowMove{
    // 是否开启
    isOpen: boolean
    // 传入要处理的窗口
    win: any
    // 窗口偏移
    winStartPosition: {
        x: number,
        y: number,
        width: number,
        height: number
    }
    // 鼠标按下时的位置
    startPosition: {
        x: number,
        y: number
    }
    constructor(){
        this.isOpen = false
        this.win = null
        this.winStartPosition = {
            x: 0,
            y: 0,
            width: 0,
            height: 0
        }
        this.startPosition = {
            x: 0,
            y: 0
        }
    }
    // 初始化
    init(win: any){
        this.win = win
    }
    // 开始移动
    start(){
        this.isOpen = true
        // 获取当前窗口偏移量[x,y]
        const winPosition = this.win.getPosition()
        // 获取当前缩放比例[width,height]
        const winSize = this.win.getSize()
        this.winStartPosition.x = winPosition[0]
        this.winStartPosition.y = winPosition[1]
        this.winStartPosition.width = winSize[0]
        this.winStartPosition.height = winSize[1]
        console.log('this.winStartPosition',this.winStartPosition)
        // 获取鼠标绝对位置
        const mousePosition = screen.getCursorScreenPoint()
        this.startPosition.x = mousePosition.x
        this.startPosition.y = mousePosition.y

        // 开启移动
        this.move()
    }
    move() {
        if(!this.isOpen){
            return
        }
        // 打印销毁和失去焦点的状态信息
        console.log('this.win.isDestroyed()',this.win.isDestroyed(),this.win.isFocused())
        // 如果窗口已销毁
        if(this.win.isDestroyed()){
            this.end()
            return
        }
        const cursorPosition = screen.getCursorScreenPoint()

        const x = this.winStartPosition.x + cursorPosition.x - this.startPosition.x
        const y = this.winStartPosition.y + cursorPosition.y - this.startPosition.y

        // this.win.setPosition(120,120,false)
        // this.win.setBounds({x:120,y:120})

        // 更新位置的同时设置窗口原大小，window上设置=>显示设置=文本缩放不是100%时，窗口会拖拽放大
        this.win.setBounds({
            x: x,
            y: y,
            width: this.winStartPosition.width,
            height: this.winStartPosition.height
        })
        // 开启下一次移动
        setTimeout(() => {
            this.move()
        }, 20)
    }
    end() {
        this.isOpen = false
    }
}